import ajax from '../../utils/ajax';

const form = document.forms.login;

form.onsubmit = form_submit;
function form_submit(e){
   e.preventDefault()
    ajax('post','/api/userLogin',{
        'uid':form.uid.value,
        'pwd':form.pwd.value,
    }).then(result => {
        if(result.flag){//登录成功
            //获取token并存储
            localStorage.setItem('token',result.data.token);
            alert(result.msg)
            //跳转到首页
            // 第一种：location = './index.html';
            //二：location.href = './index.html'
            //三：location.assign('./index.html') 
            //四：
            location.replace('/index.html') 
        }else{//登录失败
            //提示
            alert(result.msg);
            //真实重置触发
            // form.reset()
            //不真实触发
            // form.onreset()
            
            //默认选中用户名
            form.uid.select()
        } 
       
    })
}